package Logic

import (
	"encoding/json"
	"fmt"
	"gPublic/Models"
	opb "gPublic/grpc/yz-pub-data/openData"
	"strconv"
)

func getAnnexFile(logoId int) (Models.ResFile, error) {
	annexFile, err := Models.GetAuthFile(logoId, 0)
	fmt.Printf("success::::::::::::::: %#v", annexFile)
	return annexFile, err
}

func GetPcPublicData() *opb.OpenReply {
	// rd, _ := utils.Red.Get(context.Background(), "go_public_pc_public_data").Bytes()
	// fmt.Println(rd)
	// r := opb.OpenReply{}
	// err := json.Unmarshal(rd, &r)
	// if err != nil {
	// 	fmt.Println("获取redis数据失败：", err)
	// }
	// return &r
	// ----------------- 以上 redis 正常 -----------------

	r := opb.OpenReply{}
	// 获取配置参数
	webTitle := Models.GetValueByColumn("web_title")
	r.WebTitle = webTitle.ConfigValue

	polyPlay := Models.GetValueByColumn("poly_play")
	r.PolyPlay = polyPlay.ConfigValue

	webLogo := Models.GetValueByColumn("web_logo")
	logoId, _ := strconv.Atoi(webLogo.ConfigValue)

	webIco := Models.GetValueByColumn("web_ico")
	webIcoId, _ := strconv.Atoi(webIco.ConfigValue)

	if logoId > 0 {
		annexFile, err := getAnnexFile(logoId)
		annexICOFile, icoErr := getAnnexFile(webIcoId)
		if err == nil && annexFile.FilePath != "" {
			r.WebLogoPic = annexFile.FilePath
			r.WebLogoPicWidth = annexFile.Width
			r.WebLogoPicHeight = annexFile.Height
		}
		if icoErr == nil && annexICOFile.FilePath != "" {
			r.WebIcoPic = annexICOFile.FilePath
		}

	} else {
		r.WebLogoPic = "https://video.grazy.cn/lALPD2sQuEmOrenNBADNBAA_1024_1024.png"
	}

	fmt.Println("----------------------------###################----------------------------------")

	// $pcMenu	菜单 start 如果是一条数据 也就是一维数组要循环处理 必须使用反射来获取字段和值 进行处理，如果是二维也就是多条数据【切片】可以直接range处理
	webMenus, _ := Models.GetALLMenus()

	// webMenusTp := reflect.TypeOf(webMenus)
	// 输出结构体的类型信息
	// fmt.Println("webMenusTp Type:", webMenusTp)	// webMenusTp Type: []Models.GZWebMenus

	// 转指针切片
	var webMenusPtr []*Models.GZWebMenus
	for _, menu := range webMenus {
		webMenusPtr = append(webMenusPtr, &menu)
	}

	menus := Models.GetMenuTree(webMenusPtr)

	// for _, v := range menus {	// 循环打印 查看数据 包括子类是否正常
	// 	fmt.Printf("vvvvvvvvvv -------------------: %#v\n", v)
	// 	if len(v.Children) > 0 {
	// 		for _, cv := range v.Children {
	// 			fmt.Printf("cv -------------------: %#v\n", cv)
	// 		}
	// 	}
	// }

	r.WebMenus = menus
	fmt.Printf("menus -------------------: %#v", r.WebMenus) // 这里打印的是地址 正常

	var color string
	Models.DB.Table("gz_shop_style").Where("is_selected = ? and closed = ?", 1, 0).Select("color_value").Find(&color)

	// 将JSON字符串解析为map
	var data map[string]string
	err := json.Unmarshal([]byte(color), &data)
	if err != nil {
		fmt.Println("解析JSON失败:", err)
	}

	rcolor := opb.WebColor{} // opb.WebColor{}类型
	if vm, ok := data["master"]; ok {
		rcolor.Master = vm
	}

	if vs, ok := data["slave"]; ok {
		rcolor.Slave = vs
	}
	r.WebColorValue = []*opb.WebColor{&rcolor} // 创建一个包含指向 rcolor 变量地址的 opb.WebColor 类型指针的切片

	fmt.Printf("\n  data ----- : %T %v", r.WebColorValue, r.WebColorValue)

	// pc 客户
	pcservice := Models.CustomService()
	pcs := opb.CustSvc{}
	pcs.Id = int32(pcservice.Id)
	pcs.ThumbId = pcservice.ThumbId
	annexFile, err := getAnnexFile(int(pcservice.ThumbId))
	if err == nil && annexFile.FilePath != "" {
		pcs.CustomThumb = annexFile.FilePath
	}

	// p := []*opb.CustSvc{}
	// p = append(p, &pcs)
	// r.CustomService = p
	r.CustomService = []*opb.CustSvc{&pcs}

	Tel := Models.CustomTel()
	RTel := opb.Custel{}
	RTel.Id = int32(Tel.Id)
	RTel.Phone = Tel.Phone
	RTel.PromptText = Tel.PromptText
	r.CustomTel = []*opb.Custel{&RTel}

	Qq := Models.CustomQq()
	rQQ := opb.CtQQ{}
	rQQ.Id = int32(Qq.Id)
	rQQ.ThumbId = Qq.ThumbId
	rQQ.CustomThumb = ""
	r.CustomQQ = []*opb.CtQQ{&rQQ}

	// ------------------------------------------------------------------------------------------------

	GoodeCount := Models.GetGoodsCount(map[string]any{
		"goods_type": 7,
		"is_enable":  1,
		"closed":     0,
	})
	r.IsOpenVip = int32(GoodeCount)

	webMarquee := Models.GetValueByColumn("web_marquee")
	quee, _ := strconv.ParseInt(webMarquee.ConfigValue, 10, 64)
	r.IsOpenMarquee = int32(quee)

	AlipayAppAudit := Models.GetValueByColumn("alipay_app_audit")
	aaa, _ := strconv.ParseInt(AlipayAppAudit.ConfigValue, 10, 64)
	r.AlipayAudit = int32(aaa)

	payMode := Models.GetValueByColumn("pay_mode")
	if payMode.ConfigValue == "1" {
		r.PayMode = "native"
	} else {
		r.PayMode = "joinpay"
	}

	pcQQSwitch := Models.GetSwitchValueByType("pc_qq_switch")
	pcWechatSwitch := Models.GetSwitchValueByType("pc_wechat_switch")
	pcPhoneSwitch := Models.GetSwitchValueByType("pc_phone_switch")
	r.PcQqSwitch = int32(pcQQSwitch.IsEnable)
	r.PcWechatSwitch = int32(pcWechatSwitch.IsEnable)
	r.PcPhoneSwitch = int32(pcPhoneSwitch.IsEnable)

	ebookTitle := Models.GetValueByColumn("ebook_title")
	r.EbookTitle = ebookTitle.ConfigValue

	// rpa := opb.Partner{}
	partnerList := Models.GetPartners(map[string]any{
		"closed":    0,
		"is_enable": 1,
	})
	r.PartnerList = partnerList

	wechatQrcode := Models.GetValueByColumn("wechat_qrcode")
	r.WechatQrcode = wechatQrcode.ConfigValue
	if wechatQrcode.ConfigValue != "" {
		r.HasWechat = 1
	} else {
		r.HasWechat = 0
	}

	r.HasMiniwechat = 0

	webBeian := Models.GetValueByColumn("web_beian")
	webCopyRight := Models.GetValueByColumn("web_copyright")
	webSupport := Models.GetValueByColumn("web_support")
	webBaiduCode := Models.GetValueByColumn("web_baidu_code")
	r.WebBeian = webBeian.ConfigValue
	r.WebCopyright = webCopyRight.ConfigValue
	r.WebSupport = webSupport.ConfigValue
	r.WebBaiduCode = webBaiduCode.ConfigValue

	webDescription := Models.GetValueByColumn("web_description")
	webShareIco := Models.GetValueByColumn("web_share_ico")
	r.WebDescription = webDescription.ConfigValue
	if webShareIco.ConfigValue != "" {
		webShareIcoID, _ := strconv.Atoi(webShareIco.ConfigValue)
		annexFile, err := getAnnexFile(webShareIcoID)
		if err == nil && annexFile.FilePath != "" {
			r.WebShareIco = annexFile.FilePath
			r.WebShareIcoWidth = int32(annexFile.Width)
			r.WebShareIcoHeight = int32(annexFile.Height)
		}
	}

	r.WebIsClosed = 0
	r.H5IsClosed = 0
	pcH5RedirectOpen := Models.GetValueByColumn("pc_h5_redirect_open")
	pcWechatRedirectOpen := Models.GetValueByColumn("pc_wechat_redirect_open")
	h5PcRedirectOpen := Models.GetValueByColumn("h5_pc_redirect_open")

	pcH5RedirectOpenInt, _ := strconv.Atoi(pcH5RedirectOpen.ConfigValue)
	pcWechatRedirectOpenInt, _ := strconv.Atoi(pcWechatRedirectOpen.ConfigValue)
	h5PcRedirectOpenInt, _ := strconv.Atoi(h5PcRedirectOpen.ConfigValue)
	r.H5PcRedirectOpen = int32(h5PcRedirectOpenInt)
	r.PcH5RedirectOpen = int32(pcH5RedirectOpenInt)
	r.PcWechatRedirectOpen = int32(pcWechatRedirectOpenInt)

	// accountAppid := Models.GetValueByColumn("account_appid")
	r.WebIsClosed = 0
	r.H5IsClosed = 0

	// 放到缓存中
	// jsonData, err := json.Marshal(r)
	// if err != nil {
	// 	fmt.Println("转json失败")
	// }
	// rd := utils.Red.Set(context.Background(), "go_public_pc_public_data", jsonData, 3000*time.Second) // ok
	// fmt.Println("保存redis状态：", rd)
	return &r
}
